import Vue from 'vue';
import Router from 'vue-router';

import { Userinfo } from "@/storage";

import layoutHome from "@/views/layout/layoutHome.vue";
import layoutTitle from "@/views/layout/layoutTitle.vue";
import home from "./home";

import workbench from "./workbench";



var rts = [
    {
        path: '/',
        component: layoutHome,
        children: [
            ...home
        ],
    },
    {
        path: '/workbench',
        name: "workbench",
        component: layoutTitle,
        children: [
            ...workbench
        ],
    },
    {
        path: '/notification',
        name: 'notification',
        meta: {
            title: "工作台",
        },
        component: () => import(/* webpackChunkName: "notification" */'@/views/home/notification/index.vue'),
    },
    {
        path: '/login',
        name: 'login',
        meta: {
            title: "登录",
        },
        component: () => import(/* webpackChunkName: "login" */'@/views/login/index.vue'),
    },
];



Vue.use(Router);
var rt = new Router({
    mode: 'history',
    base: "/",
    routes: [
        ...rts
    ],
    scrollBehavior(to, from, saveTop) {
        return {
            x: 0,
            y: 0
        }
    }
});


rt.beforeEach((to, from, next) => {
    console.log(to)

    // 白名单不进行权限检测
    var whitelist = ["/login"];
    if (whitelist.indexOf(to.path) == -1) {
        // 判断是否登录
        var uinfo = Userinfo();
        console.log(uinfo)
        if (!uinfo || !uinfo.id) {
            next('login');
        }
    }


    var meta = to.meta || {};
    if (meta.title) {
        document.title = meta.title;
    }


    next();
})


export default rt;